﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ProgramCat
{
    /// <summary>
    /// com扩展类
    /// </summary>
    public  static class ProgramCatDllAPIHelp
    {
        /// <summary>
        /// 扩展 EPlug 添加初始化方法
        /// </summary>
        /// <param name="ePlug"></param>
        /// <returns></returns>
        public static bool Init(this EPlug ePlug)
        {
            try
            {
                //取出DLL中的Init方法 并转换为 C#托管委托类型 并进行缓存
                ePlug.initFunction = (Delegate_Init)DLLWrapper.GetFunctionAddress(ePlug.plugPtr, "Init", typeof(Delegate_Init));
                //取出DLL中的OnRequest方法 并转换为 C#托管委托类型 并进行缓存
                ePlug.onRequestFunction = (Delegate_OnRequest)DLLWrapper.GetFunctionAddress(ePlug.plugPtr, "OnRequest", typeof(Delegate_OnRequest));
                if (ePlug.initFunction == null)
                {
                    return false;
                }
                //调用DLL Init方法 
                int info_p = ePlug.initFunction.Invoke();
                if (info_p == 0)
                {
                    return false;
                }
                //拿回DLL返回的数据    因为易语言缘故 默认当作Ansi编码 蛋疼
                string info = Marshal.PtrToStringAnsi((IntPtr)info_p);
                if (String.IsNullOrEmpty(info))
                {
                    return false;
                }
                //插件名称|作者名称|版本号
                string[] infos = info.Split(new string[] { "|" }, StringSplitOptions.None);
                if (infos.Length != 3)
                    return false;
                ePlug.plugName = infos[0];
                ePlug.plugAuthor = infos[1];
                ePlug.version = infos[2];
                return true;
            }
            catch
            {
                return false;
            }
            
        }

        /// <summary>
        /// 扩展 EPlug 添加Request开始方法
        /// </summary>
        /// <param name="ePlug"></param>
        /// <param name="data">处理前的数据</param>
        /// <returns>处理后的数据</returns>
        public static byte[] OnRequest(this EPlug ePlug, byte[] data)
        {
            //先看看委托缓存有没有
            if (ePlug.onRequestFunction == null)
                return new byte[] { };
            try
            {
                //调用DLL OnRequest方法
                int _ptr = ePlug.onRequestFunction(ToInt(data), data.Length);
                //File.WriteAllText(@"C:\Users\unity\Desktop\ProgramCat\ProgramCat\bin\Release\11.txt", data.Length.ToString());
                return new byte[] { };// ToByte(_ptr);
            }
            catch(Exception e)
            {
                
                return new byte[] { };
            }
            
        }

        /// <summary>
        /// 取变量指针 
        /// </summary>
        /// <param name="data"></param>
        /// <returns>变量地址</returns>
        public static int ToInt(byte[] data)
        {
            unsafe {
                int n = 0;
                fixed (byte* p = data)
                {
                    //n = Marshal.ReadInt32((IntPtr)p);
                    n = (int)p;
                }
                    
                return n;
            }
        }
        /// <summary>
        /// 指针到字节集 - 数据地址
        /// </summary>
        /// <param name="data">数据地址</param>
        /// <returns></returns>
        public static byte[] ToByte(int data)
        {
            unsafe {
                byte* pdata = (byte*)&data;
                byte[] byteArray = new byte[sizeof(int)];
                for (int i = 0; i < sizeof(int); ++i)
                    byteArray[i] = *pdata++;
                return byteArray;
            }
        }
    }
}
